package com.ns002.yigou.quote.common.connection;

import com.ns002.yigou.quote.common.api.EBuyQuoteAPI;
import com.ns002.yigou.quote.exception.LogicException;
import com.ns002.yigou.quote.json.ParitiesQuote;
import com.ns002.yigou.quote.json.QueryQuote;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import retrofit2.Response;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xu on 2017/6/19 0019.
 * 易购数据连接
 */
@Component
public class EBuyConnection {

    @Value("${ebuy.appid}")
    public String APPID;
    @Value("${ebuy.username}")
    public String QUOTE_USERNAME;
    @Value("${ebuy.password}")
    public String QUOTE_PASSWORD;


    static Logger log = LoggerFactory.getLogger(EBuyConnection.class);

    @Autowired
    EBuyQuoteAPI eBuyQuoteAPI;

    /**
     * 登录行情服务器
     */
    public String gainPriceServerToken() {
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("client_id", "app");
            map.put("client_secret", "app_secure");
            map.put("grant_type", "password");
            map.put("username", QUOTE_USERNAME);
            map.put("password", QUOTE_PASSWORD);

            Response<Map<String, String>> response = eBuyQuoteAPI.gainPriceServerToken(map).execute();

            if (response.isSuccessful()) {
                Map<String, String> data = response.body();
                return data.get("access_token");
            } else {
                throw new LogicException("ebuy服务器出现未知错误");
            }

        } catch (IOException e) {
            e.printStackTrace();
            throw new LogicException("商城在打盹呢，请稍后再试");
        }
    }

    /**
     * 读取实时行情
     *
     * @return 返回兼容哈贵的数据
     */
    public Map<String, QueryQuote> realTime(String accessToken) {
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("access_token", accessToken);
            List<ParitiesQuote> list = eBuyQuoteAPI.gainPriceQuote(map).execute().body();
            Map<String, QueryQuote> queryQuotes = new HashMap<>();
            if (list == null || list.size() == 0) {

            } else {
                for (ParitiesQuote quote : list) {
                    QueryQuote queryQuote = new QueryQuote();
                    queryQuote.setContract(quote.getProductContract());
                    queryQuote.setCode(quote.getProductContract());
                    queryQuote.setQuote(quote.getLatestPrice().toString());
                    queryQuote.setPreClose(quote.getPriceAtEndLastday().toString());
                    queryQuote.setOpen(quote.getPriceAtBeginning().toString());
                    queryQuote.setNewDate(quote.getLatestTime());
                    queryQuote.setCreateDate(new Date());

                    queryQuote.setyQuote(quote.getPriceAtEndLastday().toString());
                    queryQuote.setHigh(quote.getHighestPrice().toString());
                    queryQuote.setLow(quote.getLowwestPrice().toString());

                    queryQuotes.put(quote.getProductContract(), queryQuote);
                }
            }

            return queryQuotes;
        } catch (IOException e) {
            e.printStackTrace();
            throw new LogicException("商城在打盹呢，请稍后再试");
        }
    }


}
